/**
 *
 *
 *
 */
import {MessageBox, Message} from "element-ui";
import axios from '../plugins/axios'

export default {
  async validateIsLogged({state, commit}) {
    let isLogged = state.isLogged
    if (isLogged) return isLogged
    isLogged = false
    commit('updateFullScreenLoading', true)
    await axios.get('/user/validateIsLogged', {})
      .then(({success, data}) => {
        isLogged = success
        if (isLogged) {
          commit('updateUserInfo', data)
        }
      })
      .finally(() => {
        commit('updateFullScreenLoading', false)
      })
    commit('updateIsLogged', isLogged)
  },
  async getUserInfo({state, commit}) {
    let userInfo = {}
    await axios.get('/user/info')
      .then(({success, data}) => {
        if (success) {
          userInfo = data
        }
      })
    commit('updateUserInfo', userInfo)
  },
  async getCategories({commit}, keyword) {
    let categories = []
    await axios.post('/category/list', {
        keyword: keyword
      }, {
        abort: true
      })
      .then(({success, message, data}) => {
        if (success) {
          categories = data
        } else {
          this.$message({
            type: 'error',
            message: message
          })
        }
      })
      .catch(({message}) => {
        this.$message({
          type: 'error',
          message: message
        })
      })
    commit('updateCategories', categories)
  },
  async getHotList({commit}) {
    let hotList = []
    await axios.get('/goods/hot/list', {
        abort: true
      })
      .then(({success, message, data}) => {
        if (success) {
          hotList = data
        }
      })
      .finally(() => {
        commit('updateHotList', hotList)
      })
  },
  async getCartList({commit}) {
    let cartList = []
    await axios.post('/cart/list', {}, {
        abort: true
      })
      .then(({success, data}) => {
        if (success) {
          cartList = data
        }
      })
      .catch(({message}) => {
        this.$message({
          type: 'error',
          message: message
        })
      })
    commit('updateCartList', cartList)
  },
  async getGoodsInfo(context, payload) {
    let {id, cartId} = payload
    const api = '/goods/info'
    let goods = {}
    if (!id) {
      axios.cancel(api)
      return goods
    }
    await axios.get('/goods/info', {
        params: {
          id: id,
          cartId: cartId
        },
        abort: true
      })
      .then(({message, data, success}) => {
        if (success) {
          goods = data
        } else {
          Message.error(message)
        }
      })
      .catch(({message}) => {
        Message.error(message)
      })
    return goods
  },
  async payOrder(context, id) {
    let flag = false
    await new Promise(resolve => {
      MessageBox.confirm('确认付款？', '订单付款', {
          type: 'warning'
        })
        .then(() => {
          axios.post('/order/pay', {
              id: id
            })
            .then(({success, message}) => {
              flag = success
              if (success) {
                Message.success('付款成功')
              } else {
                Message.error(message)
              }
            })
            .catch(({message}) => {
              Message.error(message)
            })
            .finally(resolve)
        })
        .catch(resolve)
    })
    return flag
  },
  async deleteOrder(context, id) {
    let flag = false
    await new Promise(resolve => {
      MessageBox.confirm('确认删除该订单？', '删除订单', {
        type: 'warning'
      }).then(() => {
          axios.get('/order/delete', {
              params: {
                id: id
              }
            })
            .then(({success, message}) => {
              flag = success
              if (success) {
                Message.success("删除成功");
              } else {
                Message.error(message)
              }
            })
            .catch(({message}) => {
              Message.error(message)
            })
            .finally(resolve)
        })
        .catch(resolve)
      
    })
    return flag
  },
  async getBrandList({commit}, keyword) {
    let list = []
    await axios.post('/brand/list', {
        keyword: keyword
      }, {
        abort: true
      })
      .then(({message, data, success}) => {
        if (success) {
          list = data
        }
      })
    commit('updateBrandList', list)
  },
  async getParamsList({commit}, keyword) {
    let list = []
    await axios.post('/params/list', {
        keyword: keyword
      }, {
        abort: true
      })
      .then(({message, data, success}) => {
        if (success) {
          list = data
        }
      })
    commit('updateParamsList', list)
  }
}
